package com.sto.stoapp.db.manager;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.sto.stoapp.db.StoDataBase;
import com.sto.stoapp.entity.BaseEntity;
import com.sto.stoapp.entity.SelectSignNameEntity;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.util.UnifiedStaticType;

public class SalesSignNameManager extends StoDataBase{
	private static final String SIGN_NAME_TABLE_NAME="table_sto_sign_info";
	
	private static SalesSignNameManager signNameManager;

	private static final String SQL_QUERY_ALL = "select * from table_sto_sign_info where 1=1";

	private static final String SQL_QUERY_SINGLE = "select * from table_sto_sign_info where shortKey= ? and "
			+ "nameValue=?";

	private SalesSignNameManager(Context context) {
		super(context);
	}

	/**
	 * 单例化对象
	 * 
	 * @param context
	 * @return
	 */
	public static SalesSignNameManager getInstance(Context context) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			if (signNameManager == null) {
				signNameManager = new SalesSignNameManager(context);
			}
			return signNameManager;
		}
	}

	/**
	 * 插入单条数据
	 * 
	 * @param entity
	 * @return
	 */
	public boolean insert(BaseEntity entity) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long res = -1;
			SelectSignNameEntity signNameEntity = null;
			if (entity instanceof SelectSignNameEntity) {
				signNameEntity = (SelectSignNameEntity) entity;
			} else {
				return false;
			}
			try {
				ContentValues values = new ContentValues();
				values.put("shortKey", signNameEntity.getCode());
				values.put("nameValue", signNameEntity.getName());
				values.put("userDefind", signNameEntity.getUpdateType());
				res = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE)
						.insert(SIGN_NAME_TABLE_NAME, null, values);
				if (res > 0) {
					Log.d("插入一条签名", "成功");
				} else {
					Log.d("插入一条签名", "失败");
				}
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("insert", e.getMessage());
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
			return res > 0 ? true : false;
		}
	}

	/**
	 * 批量更新数据
	 * 
	 * @param datas
	 */
	public void insertList(List<BaseEntity> datas) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			int length = datas.size();
			SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
			try {
				db.beginTransaction();
				for (int i = 0; i < length; i++) {
					long res = -1;
					SelectSignNameEntity signNameEntity = null;
					if (datas.get(i) instanceof SelectSignNameEntity) {
						signNameEntity = (SelectSignNameEntity) datas.get(i);
					} else {
						continue;
					}
					ContentValues values = new ContentValues();
					values.put("shortKey", signNameEntity.getCode());
					values.put("nameValue", signNameEntity.getName());

					//values.put(USER_WIDGET, signNameEntity.getUserWidget());
					res = db.insert(SIGN_NAME_TABLE_NAME, null, values);
					if (res > 0) {
						/*Log.d("插入一条问题件",
								"成功" + res + " = "
										+ signNameEntity.getUserWidget());*/
					} else {
						Log.d("插入一条签名", "失败");
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("insertList", e.getMessage());
			} finally {
				datas.clear();
				db.setTransactionSuccessful();
				db.endTransaction();
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
		}
	}

	public boolean querySingle(BaseEntity entity) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			boolean res = false;
			SelectSignNameEntity signNameEntity = null;
			if (entity instanceof SelectSignNameEntity) {
				signNameEntity = (SelectSignNameEntity) entity;
			} else {
				return false;
			}
			Cursor cursor = null;
			try {
				cursor = getSqliteDatabase(UnifiedStaticType.READ_DATABASE).rawQuery(
						SQL_QUERY_SINGLE,
						new String[] { signNameEntity.getCode(),
								signNameEntity.getName() });
				if (cursor != null && cursor.getCount() > 0) {
					res = true;
				} else {
					res = false;
				}
				if (cursor != null) {
					cursor.close();
				}
			} finally {
				closeCursor(cursor);
				closeDb(UnifiedStaticType.READ_DATABASE);
			}
			return res;
		}
	}
	
	/**
	 * 查询签名的数量
	 * 
	 * @return
	 */
	public int queryAllCount() {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			int count = 0;
			Cursor cursor = null;
			try {
				cursor = getSqliteDatabase(
						UnifiedStaticType.READ_DATABASE).rawQuery(
								SQL_QUERY_ALL, null);
				if (cursor != null && cursor.moveToFirst()) {
					count = cursor.getInt(0);
				}
				closeCursor(cursor);
			} finally {
				closeCursor(cursor);
				closeDb(UnifiedStaticType.READ_DATABASE);
			}
			return count;
		}
	}

	/**
	 * 删除所有数据
	 * 
	 * @return
	 */
	public boolean deleteAll() {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long res = -1;
			try {
				res = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE)
						.delete(SIGN_NAME_TABLE_NAME, null, null);
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
			return res > 0 ? true : false;
		}
	}

	/**
	 * 查询所有数据
	 * 
	 * @return
	 */
	public List<SelectSignNameEntity> queryAll() {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			List<SelectSignNameEntity> data = getDataFromCursor(getSqliteDatabase(
					UnifiedStaticType.READ_DATABASE).rawQuery(SQL_QUERY_ALL,
					null));
			return data;
		}
	}

	private List<SelectSignNameEntity> getDataFromCursor(final Cursor c) {
		List<SelectSignNameEntity> lstData = new ArrayList<SelectSignNameEntity>();
		if (c != null) {
			if (c.moveToFirst()) {
				do {
					lstData.add(getSingleData(c));
				} while (c.moveToNext());
			}
			closeCursor(c);
		}
		closeDb(UnifiedStaticType.READ_DATABASE);
		return lstData;
	}

	private SelectSignNameEntity getSingleData(Cursor c) {
		String isuser = "";
		
		SelectSignNameEntity data = new SelectSignNameEntity();
		data.setCode(c.getString(c.getColumnIndex("shortKey")));
		data.setName(c.getString(c.getColumnIndex("nameValue")));
		isuser = c.getString(c.getColumnIndex("userDefind"));
		if(isuser.equals("0")){
			data.setUser(false);
		}else{
			data.setUser(true);
		}
		
		return data;
	}
	
	public boolean delete(BaseEntity entity) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long res = -1;
			SelectSignNameEntity signNameEntity = null;
			if (entity instanceof SelectSignNameEntity) {
				signNameEntity = (SelectSignNameEntity) entity;
			} else {
				return false;
			}
			try {
				res = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE).delete(
						SIGN_NAME_TABLE_NAME,
						"shortKey=? and nameValue=?",
						new String[] { signNameEntity.getCode(),
								signNameEntity.getName() });
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
			return res > 0 ? true : false;
		}
	}
	
	/**
	 * 删除用户数据
	 * @author wxf
	 *
	 * @return
	 */
	public boolean deleteUserData() {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long res = -1;
			try {
				res = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE).delete(
						SIGN_NAME_TABLE_NAME,
						"userDefind=?",
						new String[] { "1"});
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
			return res > 0 ? true : false;
		}
	}
	
}
